﻿@section head{
    <style>
        .layui-card-body .layui-form-item {
            margin-bottom: 0px;
        }

        .layui-inline button {
            margin-right: 5px;
        }

        .layui-form-item .layui-input-inline {
            width: 200px;
            margin-right: 5px;
        }

        .layui-form-item .layui-inline {
            margin-right: 0;
        }

        @@media screen and (max-width:768px) {
            .layui-form-item .layui-input-inline {
                width: auto;
                margin-left: 5px;
            }

            .layui-inline button {
                margin-top: 5px;
            }
        }
    </style>
}
@section body {
    <div class="layui-fluid">
        <form class="layui-form" lay-filter="filter-form" id="form1">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input id="hideRoleID" class="layui-hide" value="@this.Context.Request.Query["roleID"].FirstOrDefault()" />
                            <input type="text" name="id" id="test-table-demoReload" placeholder="用户" class="layui-input">
                            <input type="text" id="Remark" name="Remark" placeholder="备注" class="layui-hide">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" class="pear-btn pear-btn-md pear-btn-primary" data-type="reload" id="btnSearch">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="button" class="pear-btn pear-btn-md pear-btn-primary" data-type="addRoleUserID">选择后关闭</button>
                    </div>
                </div>
            </div>
        </form>
        <table class="layui-hide" id="test-table-reload" lay-filter="user"></table>
    </div>

    <script type="text/html" id="switchTpl">
        <!-- 这里的 checked 的状态只是演示 -->
        <input type="checkbox" disabled name="Enabled" value="{{d.Gender}}" lay-skin="switch" lay-text="男|女" lay-filter="sexDemo" {{ d.Gender == '男' ? 'checked' : '' }}>
    </script>

    <script type="text/html" id="checkboxTpl">
        <!-- 这里的 checked 的状态只是演示 -->
        <input type="checkbox" disabled name="lock" value="{{d.Enabled}}" title="可用" lay-filter="lockDemo" {{ d.Enabled == true ? 'checked' : '' }}>
    </script>
}
@section script {

    <script>
        layui.use(['table', 'jquery', 'view'], function () {
            var table = layui.table
                , layer = layui.layer
                , view = layui.view
                , form = layui.form
                , $ = layui.$
                , index = parent.layer.getFrameIndex(window.name); //获取窗口索引;

            //方法级渲染
            table.render({
                elem: '#test-table-reload'
                , url: 'GetRoleOptionalUserListJSON'//数据接口
                , toolbar: false //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , defaultToolbar: ['filter']  //工具栏名称对象['filter', 'print', 'exports']
                , autoSort: false //禁用前端自动排序。注意：该参数为 layui 2.4.4 新增
                , page: true//开启分页
                , limit: 10
                , limits: [10, 20, 50, 100]
                , height: 'full-95' //高度最大化减去差值
                , method: "post"
                , even: false   //若不开启隔行背景，不设置该参数即可true/false
                , where: { roleID: $('#hideRoleID').val(), __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value }
                , cols: [[
                    { type: 'numbers' }
                    , { checkbox: true }
                    , { field: 'ID', width: 60, title: '序号' }
                    , { field: 'Name', width: 100, title: '用户名' }
                    , { field: 'ChineseName', width: 100, title: '中文名' }
                    , { field: 'Enabled', width: 120, title: '状态', templet: '#checkboxTpl' }
                    , { field: 'Gender', title: '性别', width: 100, templet: '#switchTpl' }
                    , { field: 'Email', width: 180, title: 'Email' }
                    , { field: 'Remark', width: 0, title: '备注' }//width:0代表占据所有列宽
                ]]
            });


            $('.layui-inline .pear-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            var active = {
                reload: function () {
                    var demoReload = $('#test-table-demoReload');
                    //执行重载
                    table.reload('test-table-reload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            key: demoReload.val(),
                            roleID: $('#hideRoleID').val(),
                            __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value
                        }
                    });
                },
                addRoleUserID: function () {
                    var checkStatus = table.checkStatus('test-table-reload')
                        , data = checkStatus.data; //获取选中的数据

                    //遍历ID，放到数组中
                    var UserIDs = new Array();
                    for (var i = 0; i < data.length; i++) {
                        UserIDs[i] = data[i].ID
                    }
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //请求登入接口
                        view.req({
                            url: 'SelectedRoleUserId' //实际使用请改成服务端真实接口
                            , data: { selectUserIDs: UserIDs, roleID: $('#hideRoleID').val(), __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value }
                            , type: "post"
                            , dataType: "json"
                            , done: function (res) {
                                if (res.codeIndex == 1) {
                                    layer.msg(res.msg, {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 1000
                                    });
                                    //调用父窗体刷新事件,不能是top窗体，有父子关系
                                    //window.parent.btnSearchClick();

                                    //加载top窗体专用，通过Iframe中id查找对应的窗体
                                    top.document.getElementById('10').contentWindow.btnSearchClick();
                                    //关闭窗体
                                    parent.layer.close(index);
                                } else {
                                    layer.msg(res.msg, {
                                        offset: 't',
                                        anim: 6
                                    });
                                }

                            }
                        });
                    }
                }
            };
        });
        document.onkeydown = function (e) {
            if (!e) e = window.event;
            if ((e.keyCode || e.which) == 13) {
                // 查找你要判断的文本框
                var myInput = document.getElementById('test-table-demoReload');
                if (myInput == document.activeElement) {
                    //获得焦点后查询
                    btnSearchClick();
                }

            }
        }
        //查询事件
        function btnSearchClick() {
            document.getElementById("btnSearch").click();
        }
    </script>
}